<?php 
include('header.php');

// Check if the user is authorized to view this
if ($authrow['level'] != 1) { header('Location: denied.php'); }

// Get posted values and insert into database (Add Server)
if ($_POST['hostname']) {
        $hostname = $_POST['hostname'];
        $check = mysql_fetch_array(mysql_query('SELECT hostname FROM server WHERE hostname=\'' . $hostname . '\''));
        if (!$check['hostname']) {
                mysql_query('INSERT INTO server (hostname) VALUES (\'' . $hostname . '\')');
		$msg = '<div class="updated">The server was created.</div>';
        } else {
		$msg = '<div class="error">The server was not created.  The message was `server already exists`.</div>';
        }
}

// Get posted values and insert perform database query (Delete/Enable/Disable)
if (isset($_POST['sAction'])) {
        $sAction = $_POST['sAction'];
	$id = $_POST['id'];
        if ($sAction == 2) {
                mysql_query('DELETE FROM server WHERE id=' . $id);
		$msg = '<div class="updated">The server was deleted.</div>';
        } elseif (($sAction == 0) || ($sAction == 1)) {
                mysql_query('UPDATE server SET disabled=' . $sAction . ' WHERE id=' . $id);
		if ($sAction == 0) {
			$msg = '<div class="updated">The server was enabled.</div>';
		} else {
			$msg = '<div class="updated">The server was disabled.</div>';
		}
        }
}

// Query database for server information
$servers = mysql_query('SELECT * FROM server ORDER BY hostname ASC'); 
?>

<h2>Server Management</h2>
<?php if (isset($msg)) { echo $msg; } // echo message if it is set?>
<hr />
<h3>Current Servers</h3>
<span class="green_bg width125">Enabled</span>
<span class="red_bg width125">Disabled</span>
<br /><br />
<table class="mediumtable">
	<tbody>
		<tr class="row1 bold">
			<td>Hostname</td>
			<td>Status</td>
			<td>Action</td>
		</tr>
		<?php $n = 0; // alternating row color increment ?>
		<?php while($serversrow = mysql_fetch_array($servers)){ //loop results ?>
		<?php // set vars ?>
		<?php $id = $serversrow['id']; ?>
		<?php $hostname = $serversrow['hostname']; ?>
		<?php $disabled = $serversrow['disabled']; ?>
		<?php
		if($n % 2 == 1) {
			$row_class = 'row1';
		} else {
			$row_class = 'row2';
		}
		?>
		<tr class="<?php echo $row_class; ?>">
			<td class="<?php if ($disabled == 0) { echo 'green_bg'; } else { echo 'red_bg'; } ?>"><a href="srvinfo.php?id=<?php echo $id; ?>"><?php echo $hostname; ?></a></td>
			<td><?php if ($disabled == 0) { echo "Enabled"; } else { echo "Disabled"; } ?></td>
			<td>
				<?php // create form for each server with unique name ?>
				<form name="sAction<?php echo $id; ?>" action="servers.php" method="post">
					<select name="sAction" onChange="sAction<?php echo $id; ?>.submit();">
						<option selected="selected">None</option>
						<option value="2">Delete</option>
						<option value="1">Disable</option>
						<option value="0">Enable</option>
					</select>
					<input type="hidden" name="id" value="<?php echo $id; ?>" />
				</form>
			</td>
		</tr>
		<?php $n++; ?>
		<?php } ?>
	</tbody>
</table>
<br />
<hr />
<h3>Add Server</h3>
<form action="servers.php" method="post">
	Hostname: <input name="hostname" type="text" size="32" />
	<input type="submit" value="Submit" />
</form>
<br />
<hr />
<?php include('footer.php'); ?>
